package com.trantor.stockmarketwatch;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class StockEntryParser {

	protected static XMLReader getXmlReader()
			throws ParserConfigurationException, SAXException {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		// create a parser
		SAXParser parser = factory.newSAXParser();
		// create the reader (scanner)
		XMLReader xmlreader = parser.getXMLReader();
		return xmlreader;
	}

	public static StockEntry parseGoogleStockEntry(String content)
			throws IOException, SAXException, ParserConfigurationException {
		XMLReader reader = getXmlReader();
		GoogleStockEntryHandler handler = new GoogleStockEntryHandler();
		reader.setContentHandler(handler);
		reader.parse(new InputSource(new StringReader(content)));
		return handler.getStockEntry();
	}
}
